package cn.liu.study.db.web;

import cn.liu.study.db.core.Result;
import cn.liu.study.db.core.ResultGenerator;
import cn.liu.study.db.model.Music;
import cn.liu.study.db.service.MusicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by liuyaqin on 2018/09/12.
*/
@RestController
@RequestMapping("/music")
public class MusicController {
    @Resource
    private MusicService musicService;

    @PostMapping
    public Result add(@RequestBody Music music) {
        musicService.save(music);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        musicService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    public Result update(@RequestBody Music music) {
        musicService.update(music);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        Music music = musicService.findById(id);
        return ResultGenerator.genSuccessResult(music);
    }

    @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Music> list = musicService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
